package com.example.service;

import com.example.consts.OrderStatus;
import com.example.pojo.dao.OrdersDao;
import com.example.pojo.dao.ResumeDao;
import com.example.pojo.entity.Orders;
import com.example.pojo.entity.Resume;
import com.example.util.OrderNumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class OrderInfoServiceImpl implements OrderInfoService{

    @Autowired
    private ResumeDao resumeDao;

    @Autowired
    private OrdersDao ordersDao;

//    生成订单信息
    @Override
    public Orders createOrderByResuId(Integer resuId,Integer jsId) {
//        查询是否有已购买且支付成功的订单
        String results = this.getAlreadyPayByResuIdAndJsId(jsId,resuId);
        if (results!=null){
            log.warn("已购买过该产品!!!");
            return null;
        }
//        查询是否有已存在但未支付的订单
        Orders orders = this.getNoPayOrderByResuId(resuId);
        if (orders!=null){
            return orders;
        }

//        获取商品信息
        Resume resume = resumeDao.findById(resuId).get();
        OrderNumUtil orderNumUtil = new OrderNumUtil(0,0);
//        生成订单
        orders = new Orders();
//        orders.setJsId();
        orders.setOrdTitle(resume.getResuName());
//      生成订单号-雪花算法
        orders.setOrdNumber(orderNumUtil.nextId());
        orders.setResuId(resuId);
        orders.setJsId(jsId);
        orders.setOrdTotalFee(resume.getResuPrice());
        orders.setOrdTime(new Date());
        orders.setOrdStatus(OrderStatus.NOTPAY.getType());  //未支付
//        orders.setIfRefund("No");
        ordersDao.saveAndFlush(orders);
        return orders;
    }

    //        TODO...相同购买用户id,相同未支付状态 + 相同金额(?)
    //    查询已存在但未支付的订单
    private Orders getNoPayOrderByResuId(Integer resuId){
//        List<Orders> ordersList = new ArrayList<>();
        List<Orders> ordersList = ordersDao.findByOrdStatus(OrderStatus.NOTPAY.getType());
        for(Orders o:ordersList){
            if (o.getResuId().compareTo(resuId)==0 & o.getOrdStatus().equals(OrderStatus.NOTPAY.getType())){
                Orders orders = o;
                return orders;
            }
        }
        return null;
    }

//    查询已存在且已支付的订单-防止重复购买
    private String getAlreadyPayByResuIdAndJsId(Integer jsId,Integer resuId){
        List<Orders> ordersbuyList = ordersDao.findByResuIdAndJsId(jsId,resuId);
        String result = null;
        for(Orders o:ordersbuyList){
            if (o.getOrdStatus().equals(OrderStatus.SUCCESS.getType())){
                result = "已存在订单";
            }
        }
        return result;
    }

//    由订单号查找对应订单
    @Override
    public Orders getOrdbyOrdNum(Long ordTradeNum) {
        Orders result = ordersDao.findByOrdNumber(ordTradeNum);
        return result;
    }

//    更新订单状态
    @Override
    public void updateStatusByOrdNum(Long ordNum,String newStatus) {
        System.out.println("订单状态更新");
        System.out.println(newStatus);
        ordersDao.updateOrdStatusByOrdNumber(ordNum,newStatus);
    }

//    获取订单状态
    @Override
    public String getOrderStatus(Long orderNum) {
        String orderStatus = ordersDao.findOrdStatusByOrdNumber(orderNum);
        return orderStatus;
    }

//    获取规定时间内未支付的订单
    @Override
    public List<Orders> getNoPayOrderByDuration(Integer id,int minutes) {
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, -5);
        Date beforeDate = beforeTime.getTime();
        List<Orders> ordersList = ordersDao.findByOrdStatusAndId(id,OrderStatus.NOTPAY.getType());
        List<Orders> ordersList1 = new ArrayList<>();
        for (Orders o : ordersList) {
            if (o.getOrdStatus().equals(OrderStatus.NOTPAY.getType()) && o.getOrdTime().compareTo(beforeDate) > 0){
                ordersList1.add(o);
            }
        }
        return ordersList1;
    }

    @Override
    public String getByOrdTitleAndJsId(String ordTitle,Integer jsId) {
        List<Orders> ordersList = ordersDao.findByOrdTitleAndJsId(ordTitle,jsId);
        String ordNum = null;
        System.out.println(ordersList+"-------------------------------");
        if (ordersList==null){
            return null;
        }
        for (Orders o : ordersList) {
            if (o.getOrdStatus().equals(OrderStatus.SUCCESS.getType())) {
                ordNum = o.getOrdNumber().toString();
                System.out.println(ordNum);
            }
        }
        return ordNum;
    }

    @Override
    public List<Orders> getByJsId(Integer jsId) {
        List<Orders> ordersList1 = ordersDao.findByJsId(jsId);
        List<Orders> ordersList = new ArrayList<>();
        if (ordersList1 != null){
            for (Orders o : ordersList1) {
               if (o.getOrdStatus().equals(OrderStatus.NOTPAY.getType())){
                   ordersList.add(o);
               }
            }
        }
        return ordersList;
    }


//    修改订单信息
//    @Override
//    public void createPaymentInfo(Map<String, String> params) {
//        String OrdNum1 = params.get("out_trade_no");
//        String transactionId = params.get("trade_no");
//        String tradeStatus = params.get("trade_status");
//        String total = params.get("total_amount");
//        Double totalAmount = Double.parseDouble(total);
//
//        Long OrdNum = Long.parseLong(OrdNum1);
//        Orders orders = new Orders();
//        orders.setOrdNumber(OrdNum);
//        orders.setTransactionId(transactionId);
//        orders.setTradeType("电脑网站支付");
//        orders.setOrdStatus(tradeStatus);
//        orders.setOrdTotalFee(totalAmount);
//    }


}
